package batchcal.mas.dtc.util.baidu;

import batchcal.mas.dtc.scripts.outsource.source.impl.HttpDataSource;
import batchcal.mas.dtc.util.baidu.entity.HotelSoaEntity;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author: ycy
 * @CreateDate: 2018/8/27 15:28
 */
public class HotelSOAUtil {

   static String url = "http://10.28.101.212:8080/api/json/getRecommendArticleInfoList";
  /**
   * 日志
   */
  protected static final Logger LOG = LoggerFactory.getLogger(HttpDataSource.class);

  public static void main(String[] args) {
   ;
    System.out.println( getRes(2));
  }

  public  static String  getRes(Integer cityid){
    try {
      Thread.sleep(100);
      RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(5000)// 一、连接超时：connectionTimeout-->指的是连接一个url的连接等待时间
              .setSocketTimeout(5000)// 二、读取数据超时：SocketTimeout-->指的是连接上一个url，获取response的返回等待时间
              .setConnectionRequestTimeout(5000).build();

      HttpClient hc = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
//http://webapi.soa.ctripcorp.com/api/14137/json/getRecommendArticleInfoList
      HotelSoaEntity hotelSoaEntity = new HotelSoaEntity(1,cityid,1,30);
      Object json = JSONObject.toJSON(hotelSoaEntity);
      StringEntity s = new StringEntity(json.toString());
      HttpPost post =  new HttpPost(url);
      post.setEntity(s);
      HttpResponse fortressResp = hc.execute(post);
      if (fortressResp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        String res = EntityUtils.toString(fortressResp.getEntity());
        return res;
      }
    } catch (Exception e) {
      LOG.error(e.getMessage());

    }
    return null;
  }

}

